<?php
/*
 * Created on Jun 16, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once(dirname(__FILE__)."/../../config_dbentity.php");
require_once(DBENTITY);

class PhporcupineFacebook extends DBEntity
{
	# Informazioni applicazione
	const APP_ID = '123918284297439';
	const API_KEY = '3542609fbb1bfae6b4f3d9215a3c1674';
	const APP_SECRET = '7d569ba2fa597763ca2f5fa3d668000f';
	 
	
	# Informazioni utente
	public $api_call, $fb_user, $facebook, $user; 
	
	const TABLE = "phporcupine_facebook_accounts";
	const PRIMARY_KEY = "account_id";
	
	
	public function __construct(User $user)
	{
		// Recupero le informazioni(app_id, app_secret, app_appname) dal database
		
		# debug
	#	echo "Costruzione oggetto 'PhporcupineFacebook' in corso...<br />";
	#	echo "Utente: {$user->user_id}<br /> oauth_id: {$user->fb_oauth_uid}<br /><br />";
	#	echo "Costruzione oggetto 'Api Client Facebook' in corso...<br />";
		
		$this->user = $user;
		# Create facebook object
		$this->facebook = new Facebook(self::API_KEY, self::APP_SECRET);
		$this->facebook->api_client->session_key = $user->fb_session_key;
		
	#	echo "Session_key: ";
	#	var_dump($user->fb_session_key);
	#	echo "<br />";
		
		# Debug
	#	echo "Oggetto facebook: "; var_dump($this->facebook); echo "<br />";
		//echo "Chiave api: ".$this->facebook->api_key."<br />";
		# Verify that user is existing
	//	$this->fb_user = $this->facebook->api("/".$user->fb_oauth_uid);
		
		# Debug
	//	echo "Informazioni utente: ";var_dump($this->fb_user);echo "<br />";
		//echo '$this->api->api("/"'.$user->fb_oauth_uid.'): '; var_dump($this->fb_user); echo "<br />";
	}
	
	public function publishBlogpost(Blogpost $blogpost)
	{
		$can_post = 0;
		$result_post = 0;
		try{
			
			
			$message = "Nuovo articolo pubblicato";
			$attachments = array(
				'name'		=>	"{$blogpost->blogpost_title}",
				'href'		=> 	"",
				'caption'	=>	"{$blogpost->blogpost_subtitle}",
				'description'=>	"{$blogpost->blogpost_body}"
			/*	'media'		=>	array(
					array(
						'type'	=>	'image',
						'src'	=>	'http://www.trentinoweb.it/blog/wp-content/uploads/2009/08/alcol-aperitivo.jpg',
						'href'	=>	'http://www.fondocecconi.it/2010/05/26/aperitivo-dibattito/'
					)
				)
			*/
			);		
		/*	$action_links = array(
				array(
					'text'	=>	"Ricappa",
					'href'	=>	"http://www.fondocecconi.it"
				)
			);
		*/
			$attachments = json_encode($attachments);
		//	$action_links = json_encode($action_links);
		//	$target_id = 124504587569541 ;
			if($this->facebook->api_client->stream_publish($message, $attachments))
				{
					$result_post = 1; 
				}
		} catch(Exception $e) {
			echo $e ."<br />";
		}
		return $result_post;
	}
	
}

?>
